package com.asm.DBloader;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ASM_FDaynPrdHedDS {
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;
	
	
	public ASM_FDaynPrdHedDS(Context context) {
		this.context = context;
		dbHelper  = new ASM_DB(context);
		
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	public int Create(ASM_FDaynPrdHed det)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.asmFnonHed_RefNo, det.getAsmFnonHed_RefNo());
		values.put(ASM_DB.asmFnonHed_TxnDate, det.getAsmFnonHed_TxnDate());
		values.put(ASM_DB.asmFnonHed_DealCode, det.getAsmFnonHed_DealCode());
		values.put(ASM_DB.asmFnonHed_RepCode, det.getAsmFnonHed_RepCode());
		values.put(ASM_DB.asmFnonHed_Remarks, det.getAsmFnonHed_Remarks());
		values.put(ASM_DB.asmFnonHed_CostCode, det.getAsmFnonHed_CostCode());
		values.put(ASM_DB.asmFnonHed_AddUser, det.getAsmFnonHed_AddUser());
		values.put(ASM_DB.asmFnonHed_AddDate, det.getAsmFnonHed_AddDate());
		values.put(ASM_DB.asmFnonHed_AddMach, det.getAsmFnonHed_AddMach());
		values.put(ASM_DB.asmFnonHed_RecordId, det.getAsmFnonHed_RecordId());
		values.put(ASM_DB.asmFnonHed_TranBatch, det.getAsmFnonHed_TranBatch());
		values.put(ASM_DB.asmFnonHed_Longitude, det.getAsmFnonHed_Longitude());
		values.put(ASM_DB.asmFnonHed_Latitude, det.getAsmFnonHed_Latitude());
		values.put(ASM_DB.asmFnonHed_Issynced, det.getAsmFnonHed_Issynced());
		values.put(ASM_DB.asmFnonHed_Address, det.getAsmFnonHed_Address());
		count = (int) dB.insert(ASM_DB.ASM_FDAYNPRDHED, null, values);

		dB.close();
		return count;
		
	}
	public ArrayList<String> getfDaynPrdHed(String refNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select * from FDaynPrdHed where RefNo = '"+refNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String RefNo = cursor.getString(1);
			String TxnDate = cursor.getString(2);
			String DealCode = cursor.getString(3);
			String RepCode = cursor.getString(4);
			String Remarks = cursor.getString(5);
			String CostCode = cursor.getString(6);
			String AddUser = cursor.getString(7);
			String AddDate = cursor.getString(8);
			String AddMach = cursor.getString(9);
			String RecordId = cursor.getString(10);	
			String TranBatch = cursor.getString(11);
			
			
			Details.add(RefNo);
			Details.add(TxnDate);
			Details.add(DealCode);
			Details.add(RepCode);
			Details.add(Remarks);
			Details.add(CostCode);
			Details.add(AddUser);
			Details.add(RepCode);
			Details.add(AddDate);
			Details.add(AddMach);
			Details.add(RecordId);
			Details.add(TranBatch);
			
			
			
		}
		
		return Details;
	}
	
	
	public ArrayList<String> getfDaynPrdHedall() 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select RefNo from FDaynPrdHed";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String RefNo = cursor.getString(0);
			Details.add(RefNo);
	
			
			
			
		}
		
		return Details;
	}
	
	public int Delete(String Refno)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FDAYNPRDHED, "RefNo = '"+Refno+"'", null);

		dB.close();
		return count;
		
	}
	
	public int Update(ASM_FDaynPrdHed det)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.asmFnonHed_RefNo, det.getAsmFnonHed_RefNo());
		values.put(ASM_DB.asmFnonHed_TxnDate, det.getAsmFnonHed_TxnDate());
		values.put(ASM_DB.asmFnonHed_DealCode, det.getAsmFnonHed_DealCode());
		values.put(ASM_DB.asmFnonHed_RepCode, det.getAsmFnonHed_RepCode());
		values.put(ASM_DB.asmFnonHed_Remarks, det.getAsmFnonHed_Remarks());
		values.put(ASM_DB.asmFnonHed_CostCode, det.getAsmFnonHed_CostCode());
		values.put(ASM_DB.asmFnonHed_AddUser, det.getAsmFnonHed_AddUser());
		values.put(ASM_DB.asmFnonHed_AddDate, det.getAsmFnonHed_AddDate());
		values.put(ASM_DB.asmFnonHed_AddMach, det.getAsmFnonHed_AddMach());
		values.put(ASM_DB.asmFnonHed_RecordId, det.getAsmFnonHed_RecordId());
		values.put(ASM_DB.asmFnonHed_TranBatch, det.getAsmFnonHed_TranBatch());

		count = (int) dB.update(ASM_DB.ASM_FDAYNPRDHED, values, "RefNo = '"+det.getAsmFnonHed_RefNo().toString()+"'", null);

		dB.close();
		return count;
		
	}
}
